package com.data.modules.es.controller;

import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.aliyuncs.utils.StringUtils;
import com.data.common.api.vo.Result;
import com.data.modules.es.entity.ACSDocument;
import com.data.modules.es.mapper.DocumentMapper;
import com.data.modules.es.service.IAcsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@SuppressWarnings("ALL")
@Slf4j
@Api(tags = "ACS-ES")
@RestController
@RequestMapping("/openapi/acs/")
public class AcsESController {
    @Autowired
    private IAcsService acsBiz;
    @Resource
    private DocumentMapper esDocument;


    /**
     * 初始化插入数据,默认开启自动挡,自动挡模式下,索引会自动创建及更新. 若未开启自动挡,则在此步骤前需先调用创建索引API完成索引创建
     *
     * @return
     */
    @ApiOperation(value = "新增数据至索引", notes = "新增数据至索引")
    @GetMapping("insertBatchDB2ES")
    public Result<?> insertBatchDB2ES() {
        long start = System.currentTimeMillis();
        acsBiz.insertBatchDB2ES();
        log.info("===insertBatchDB2ES coust:{}",(System.currentTimeMillis() - start));
        return Result.ok("ES初始化成功");
    }

    /**
     * 演示根据标题精确查询文章
     * 例如title传值为 我真帅,那么在当前配置的索引下,所有标题为'我真帅'的文章都会被查询出来
     * 其它各种场景的查询使用,请移步至test模块
     *
     * @param title
     * @return
     */
    @ApiOperation(value = "获取数据", notes = "获取数据")
    @GetMapping("/listDocumentByTitle")
    public Result<?>  listDocumentByTitle(@RequestParam(required = false) String title,@RequestParam(required = false) Integer pageNum) {
        long start = System.currentTimeMillis();
        //TODO 记录查询请求日志  TODO 结合推荐
        if(StringUtils.isEmpty(title)){
            title = "测试标题";
        }

        PageInfo<ACSDocument> list = acsBiz.pageQuery(title,pageNum);

        log.info("===listDocumentByTitle coust:{}",(System.currentTimeMillis() - start));
        return Result.ok(list);
    }


    /**
     *
     * @param content
     * @return
     */
    @ApiOperation(value = "高亮查询测试", notes = "自定义注解指定高亮返回字段,高亮查询测试")
    @GetMapping("/highlightSearch")
    public Result<?> highlightSearch(@RequestParam(required = false) String content,@RequestParam(required = false) Integer pageNum) {
        //TODO 记录查询请求日志  TODO 结合推荐
        long start = System.currentTimeMillis();
        PageInfo<ACSDocument> list = acsBiz.pageHighlightSearch(content,pageNum);
        log.info("===highlightSearch coust:{}",(System.currentTimeMillis() - start));
        return Result.OK(list);
    }


    /**
     * 演示根据title删除文章，同时会被 DeleteInterceptor 拦截，执行逻辑删除
     *
     * @param title
     * @return
     */
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @GetMapping("/deleteDocumentByTitle")
    public Integer deleteDocumentByTitle(@RequestParam String title) {
        // 实际开发中会把这些逻辑写进service层 这里为了演示方便就不创建service层了
        LambdaEsQueryWrapper<ACSDocument> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(ACSDocument::getId,"1");
        return esDocument.delete(wrapper);
    }

}